<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class CountsModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->session=$this->session();
    }

    /*
     * 商品列表
     * */
    public function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
    public function index($cause)
    {
        /*条件编写*/
        $session=$this->session();
        $where='shop_id='.$session['id'];
        if(isset($cause['goods_id']))
        {
            if($cause['goods_id'])
            {
                $where .=" and goods_id =".$cause['goods_id'];
            }
        }else{
            $cause['order_num']='';
        }
        $why="";
        if(isset($cause['goods_name']))
        {
            if($cause['goods_name'])
            {
               $why['goods_name']=array('like',"%".$cause['goods_name']."%");
            }
        }else{
            $cause['goods_name']='';
        }
        if(isset($cause['start_time']) && isset($cause['end_time']))
        {
            if(!empty($cause['start_time']))
            {
               $start=strtotime($cause['start_time']);
               if(!empty($cause['end_time']))
               {
                   $end=strtotime($cause['end_time']);
               }else{
                   $end=$start+86400;
               }
               $where.=' and w_time  >='.$start;
               $where.=' and w_time  <='.$end;
            }
        }else{
            $cause['start_time']='';
            $cause['end_time']='';
        }
        if(isset($cause['order_by']))
        {
            if($cause['order_by']==1)
            {
                $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num) as number')->order('sum(money) asc')->paginate(10);
            }else{
                $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num)  as number')->order('sum(money) desc')->paginate(10);
            }
        }else{
                $data=DB::name('order_detail')->alias('or')->whereOr($why)->where($where)->group('goods_id')->field('or.*,sum(money) as total_money,count(num)  as number')->paginate(10);
        }
        /*对数据进行封装*/
        if($data->toArray())
        {

            foreach ($data as $k=>$v)
            {
                $date=array();
                $date=$v;
                if($date['stemeal']==1)
                {
                    $date['stemeal_name']='单品';
                }else{
                    $date['stemeal_name']='套餐';
                }
                $data->offsetSet($k,$date);

            }
        }
        /*分页保存条件*/
        $data->appends($cause);
        return $data;
    }

}